//封装 axios 做一些公共配置
import axios from 'axios'

axios.defaults.baseURL = 'http://47.97.31.20:3000'
axios.defaults.headers.post['Content-Type'] = 'application/json'//告诉浏览器后端返回的数据格式是json格式


// 请求拦截器
axios.interceptors.request.use(req => {
    let token = localStorage.getItem('token') || sessionStorage.getItem('token')
    if (token) {
      req.headers.Authorization = token
    }
    return req
  })


//响应拦截
axios.interceptors.response.use(res => {
    if(res.status !==200 ){//程序性错误
        ElMessage.error('服务器错误')
        return Promise.reject(res)
    }else{
        if(res.data.code === 401){//没有登陆
            ElMessage.error(res.data.msg)
            return Promise.reject(res)

        }
        if(res.data.code !== 200){//业务错误
            ElMessage.error(res.data.msg)
            return Promise.reject(res)
        }

        return res.data
    }
})

export default axios